package com.example.chenxiaojun.encrption;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

//对称加密 DES(8位),AES(16位),3DES(24位)
public class MainActivity extends AppCompatActivity {
    private EditText input;
    private EditText inputPassword;
    private TextView showinfo;
    private String data;
    private String pw;
    private String result;
    private String decryptResult = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        input = (EditText) findViewById(R.id.input);
        inputPassword = (EditText) findViewById(R.id.inputpassWord);
        showinfo = (TextView) findViewById(R.id.showinfo);
    }

    public void decrypt(View view) {

        switch (view.getId()) {
            case R.id.desDecrypt:
                decryptResult = decrypt("DES", result, pw);
                showinfo.setText(decryptResult);
                break;
            case R.id.dddesDecrypt:
                decryptResult = decrypt("DESede", result, pw);
                showinfo.setText(decryptResult);

                break;
            case R.id.aesDecrypt:
                decryptResult = decrypt("AES", result, pw);
                showinfo.setText(decryptResult);
                break;
        }
    }

    public void encrypt(View view) {
        data = input.getText().toString().trim();
        pw = inputPassword.getText().toString().trim();
        switch (view.getId()) {
            case R.id.desEncrypt:
                result = encrypt("DES", data, pw);
                showinfo.setText(data + "," + result);
                break;
            case R.id.dddesEncrypt:
                result = encrypt("DESede", data, pw);
                showinfo.setText(data + "," + result);
                break;
            case R.id.aesEncrypt:
                result = encrypt("AES", data, pw);
                showinfo.setText(data + "," + result);
                break;
        }
    }

    private String decrypt(String type, String data, String pw) {
        Cipher cipher = null;
        Key key = null;
        switch (type) {
            case "DES":
                try {
                    cipher = Cipher.getInstance(type);
                    if (pw.getBytes().length != 8) {
                        throw new Exception("密码长度必须是八位");
                    }
                    key = new SecretKeySpec(pw.getBytes(), type);
                    cipher.init(Cipher.DECRYPT_MODE, key);
                    if (!TextUtils.isEmpty(data)) {
                        byte[] base64decode = Base64.decode(data.getBytes(), Base64.DEFAULT);
                        byte[] decryptBytes = cipher.doFinal(base64decode);

                        return new String(decryptBytes);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "DESede":
                try {
                    cipher = Cipher.getInstance(type);
                    if (pw.getBytes().length != 24) {
                        throw new Exception("密码长度必须是八位");
                    }
                    key = new SecretKeySpec(pw.getBytes(), type);
                    cipher.init(Cipher.DECRYPT_MODE, key);
                    if (!TextUtils.isEmpty(data)) {
                        byte[] base64decode = Base64.decode(data.getBytes(), Base64.DEFAULT);
                        byte[] decryptBytes = cipher.doFinal(base64decode);

                        return new String(decryptBytes);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "AES":
                try {
                    cipher = Cipher.getInstance(type);
                    if (pw.getBytes().length != 16) {
                        throw new Exception("密码长度必须是八位");
                    }
                    key = new SecretKeySpec(pw.getBytes(), type);
                    cipher.init(Cipher.DECRYPT_MODE, key);
                    if (!TextUtils.isEmpty(data)) {
                        byte[] base64decode = Base64.decode(data.getBytes(), Base64.DEFAULT);
                        byte[] decryptBytes = cipher.doFinal(base64decode);

                        return new String(decryptBytes);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            default:
                try {
                    throw new Exception("加密类型不匹配");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

        }
        return null;
    }


    //三种方式
    private String encrypt(String type, String data, String pw) {
        Cipher cipher = null;
        Key key = null;
        switch (type) {
            case "DES":
                try {
                    cipher = Cipher.getInstance(type);
                    if (pw.getBytes().length != 8) {
                        throw new Exception("密码长度必须是八位");
                    }
                    key = new SecretKeySpec(pw.getBytes(), type);
                    cipher.init(Cipher.ENCRYPT_MODE, key);
                    if (!TextUtils.isEmpty(data)) {

                        byte[] encryptBytes = cipher.doFinal(data.getBytes());
                        byte[] base64encode = Base64.encode(encryptBytes, Base64.DEFAULT);
                        return new String(base64encode);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "DESede":
                try {
                    cipher = Cipher.getInstance(type);
                    if (pw.getBytes().length != 24) {
                        throw new Exception("密码长度必须是24位");
                    }
                    key = new SecretKeySpec(pw.getBytes(), type);
                    cipher.init(Cipher.ENCRYPT_MODE, key);
                    if (!TextUtils.isEmpty(data)) {
                        byte[] encryptBytes = cipher.doFinal(data.getBytes());
                        byte[] base64encode = Base64.encode(encryptBytes, Base64.DEFAULT);
                        return new String(base64encode);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "AES":
                try {
                    cipher = Cipher.getInstance(type);
                    if (pw.getBytes().length != 16) {
                        throw new Exception("密码长度必须是16位");
                    }
                    key = new SecretKeySpec(pw.getBytes(), type);
                    cipher.init(Cipher.ENCRYPT_MODE, key);
                    if (!TextUtils.isEmpty(data)) {
                        byte[] encryptBytes = cipher.doFinal(data.getBytes());
                        byte[] base64encode = Base64.encode(encryptBytes, Base64.DEFAULT);
                        return new String(base64encode);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            default:
                try {
                    throw new Exception("加密类型不匹配");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

        }
        return null;
    }
}
